/*
 * Common Unit Testing Functions
 * Copyright (C) 2003 Johnathan Ingram, jingram@rogueware.org
 *
 * This library is free software; you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public
 *   License as published by the Free Software Foundation; either
 *   version 2.1 of the License, or (at your option) any later version.
 *
 *   This library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public
 *   License along with this library; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  US
 *
 */
 
 

#include "common.h"

#include "baseValue.h"

int 
main(
      int argc, 
      char **argv)
{
   printTestHeader("BaseValue Object");
   

   JDate now;
   BaseValue *baseValue = NULL;
   
   // Test asString
   baseValue = new BaseValue("StringField");
   cout << INDENT << "TESTING: asString" << endl;
   
   cout << INDENT2 << "setString(\"Testing String\"):" << INDENT;
   baseValue->setString("Testing String");
   cout << baseValue->asString() << endl;
   
   cout << INDENT2 << "setLong(-69200):" << INDENT;
   baseValue->setLong(-69200);
   cout << baseValue->asString() << endl;
   
   cout << INDENT2 << "setUnsignedLong(1200000):" << INDENT;
   baseValue->setUnsignedLong(1200000);
   cout << baseValue->asString() << endl;

   cout << INDENT2 << "setFloat(84562.9932849):" << INDENT;
   baseValue->setFloat(84562.9932849);      // This will always return the true IEEE internal rounding error storage as a string.
   cout << baseValue->asString() << endl;

   cout << INDENT2 << "setDateTime( " << now.asString().c_str() << "):" << INDENT;
   baseValue->setDateTime(now);
   cout << baseValue->asString() << endl;

   cout << INDENT2 << "setBoolean(true)" << INDENT;
   baseValue->setBoolean(true);
   cout << baseValue->asString() << endl;

   cout << INDENT2 << "setBinary((void*)\"ABC\", 4)" << INDENT;
   baseValue->setBinary((void*)"ABC", 4);
   cout << baseValue->asString() << endl;
   
   delete baseValue;
   // End asString   
   
   
   // Test asLong
   baseValue = new BaseValue("LongField");
   cout << INDENT << "TESTING: asLong" << endl;

   cout << INDENT2 << "setString(\"-9000001\"):" << INDENT;
   baseValue->setString("-9000001");
   cout << baseValue->asLong() << endl;
   
   cout << INDENT2 << "setLong(-69200):" << INDENT;
   baseValue->setLong(-69200);
   cout << baseValue->asLong() << endl;
   
   cout << INDENT2 << "setUnsignedLong(1200000):" << INDENT;
   baseValue->setUnsignedLong(1200000);
   cout << baseValue->asLong() << endl;
   
   cout << INDENT2 << "setFloat(84562.9932849):" << INDENT;
   baseValue->setFloat(84562.9932849);      
   cout << baseValue->asLong() << endl;

   cout << INDENT2 << "setDateTime( " << now.asString().c_str() << "):" << INDENT;
   baseValue->setDateTime(now);
   cout << baseValue->asLong() << " UnixTimestamp. 0 if out of range" << endl;

   cout << INDENT2 << "setBoolean(true)" << INDENT;
   baseValue->setBoolean(true);
   cout << baseValue->asLong() << endl;

   cout << INDENT2 << "setBinary((void*)\"ABC\", 4)" << INDENT;
   baseValue->setBinary((void*)"ABCEFG", 4);
   cout << baseValue->asLong() << endl;

   delete baseValue;
   // End asLong   


   // Test asUnsignedLong
   baseValue = new BaseValue("UnsignedLongField");
   cout << INDENT << "TESTING: asUnsignedLong" << endl;

   cout << INDENT2 << "setString(\"84888223\"):" << INDENT;
   baseValue->setString("834888223");
   cout << baseValue->asUnsignedLong() << endl;
   
   cout << INDENT2 << "setLong(-69200):" << INDENT;
   baseValue->setLong(-69200);
   cout << baseValue->asUnsignedLong() << endl;

   cout << INDENT2 << "setLong(100000):" << INDENT;
   baseValue->setLong(100000);
   cout << baseValue->asUnsignedLong() << endl;
   
   cout << INDENT2 << "setUnsignedLong(1200000):" << INDENT;
   baseValue->setUnsignedLong(1200000);
   cout << baseValue->asUnsignedLong() << endl;
   
   cout << INDENT2 << "setFloat(84562.9932849):" << INDENT;
   baseValue->setFloat(84562.9932849);      
   cout << baseValue->asUnsignedLong() << endl;

   cout << INDENT2 << "setDateTime( " << now.asString().c_str() << "):" << INDENT;
   baseValue->setDateTime(now);
   cout << baseValue->asUnsignedLong() << " UnixTimestamp. 0 if out of range" << endl;

   cout << INDENT2 << "setBoolean(false)" << INDENT;
   baseValue->setBoolean(false);
   cout << baseValue->asUnsignedLong() << endl;

   cout << INDENT2 << "setBinary((void*)\"ABC\", 4)" << INDENT;
   baseValue->setBinary((void*)"ABCEFG", 4);
   cout << baseValue->asUnsignedLong() << endl;

   delete baseValue;
   // End asLong   


   // Test asFloat
   baseValue = new BaseValue("FloatField");
   cout << INDENT << "TESTING: asFloat" << endl;

   cout.precision(12);  // Make sure all the floating point is printed

   cout << INDENT2 << "setString(\"841.942056\"):" << INDENT;
   baseValue->setString("841.942056");
   cout << baseValue->asFloat() << endl;
   
   cout << INDENT2 << "setLong(-69200):" << INDENT;
   baseValue->setLong(-69200);
   cout << baseValue->asFloat() << endl;
   
   cout << INDENT2 << "setUnsignedLong(1200000):" << INDENT;
   baseValue->setUnsignedLong(1200000);
   cout << baseValue->asFloat() << endl;
   
   cout << INDENT2 << "setFloat(84562.993284):" << INDENT;
   baseValue->setFloat(84562.993284);      
   cout << baseValue->asFloat() << endl;

   cout << INDENT2 << "setDateTime( " << now.asString().c_str() << "):" << INDENT;
   baseValue->setDateTime(now);
   cout << baseValue->asFloat() << " Julian Day Number, see JDate" << endl;

   cout << INDENT2 << "setBoolean(true)" << INDENT;
   baseValue->setBoolean(true);
   cout << baseValue->asFloat() << endl;

   cout << INDENT2 << "setBinary((void*)\"ABC\", 4)" << INDENT;
   baseValue->setBinary((void*)"ABCEFG", 4);
   cout << baseValue->asFloat() << endl;

   delete baseValue;
   // End asFloat   



   // Test asDateTime
   baseValue = new BaseValue("DateTimeField");
   cout << INDENT << "TESTING: asDateTime" << endl;

   cout << INDENT2 << "setString(\"2003-01-31 19:15:31\"):" << INDENT;
   baseValue->setString("2003-01-31 19:15:31");
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;

   cout << INDENT2 << "setString(\"19:15:31\"):" << INDENT;
   baseValue->setString("19:15:31");
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;

   cout << INDENT2 << "setString(\"2003-01-31 19:15:31+02\"):" << INDENT;
   baseValue->setString("2003-01-31 19:15:31+02");
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;

   cout << INDENT2 << "setString(\"12:00:00-08\"):" << INDENT;
   baseValue->setString("12:00:00-08");
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;

   cout << INDENT2 << "setLong(0):" << INDENT;
   baseValue->setLong(0);
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;
   
   cout << INDENT2 << "setUnsignedLong(1061032147):" << INDENT;
   baseValue->setUnsignedLong(1061032147);
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;
   
   cout << INDENT2 << "setFloat(2465442.5):" << INDENT;
   baseValue->setFloat(2465442.5);      
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;

   cout << INDENT2 << "setDateTime( " << now.asString().c_str() << "):" << INDENT;
   baseValue->setDateTime(now);
   cout << baseValue->asDateTime().asString().c_str() << " UTC" << endl;

   cout << INDENT2 << "setBoolean(true)" << INDENT;
   baseValue->setBoolean(true);
   cout << baseValue->asDateTime().asString().c_str() << " UTC   JDate Epoch" << endl;

   cout << INDENT2 << "setBinary((void*)\"ABC\", 4)" << INDENT;
   baseValue->setBinary((void*)"ABCEFG", 4);
   cout << baseValue->asDateTime().asString().c_str() << " UTC   JDate Epoch" << endl;

   delete baseValue;
   // End asDateTime   



   // Test asBoolean
   baseValue = new BaseValue("BoolField");
   cout << INDENT << "TESTING: asBoolean" << endl;

   cout << INDENT2 << "setString(\"true\"):" << INDENT;
   baseValue->setString("true");
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setString(\"T\"):" << INDENT;
   baseValue->setString("T");
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setString(\"F\"):" << INDENT;
   baseValue->setString("F");
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setString(\"bla\"):" << INDENT;
   baseValue->setString("bla");
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setString(\"22\"):" << INDENT;
   baseValue->setString("22");
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setString(\"-22\"):" << INDENT;
   baseValue->setString("-22");
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setLong(1):" << INDENT;
   baseValue->setLong(1);
   cout << baseValue->asBoolean() << endl;
   
   cout << INDENT2 << "setUnsignedLong(0):" << INDENT;
   baseValue->setUnsignedLong(0);
   cout << baseValue->asBoolean() << endl;
   
   cout << INDENT2 << "setFloat(5):" << INDENT;
   baseValue->setFloat(5);      
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setDateTime( " << now.asString().c_str() << "):" << INDENT;
   baseValue->setDateTime(now);
   cout << baseValue->asBoolean() << " Always false" << endl;

   cout << INDENT2 << "setBoolean(true)" << INDENT;
   baseValue->setBoolean(true);
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setBoolean(false)" << INDENT;
   baseValue->setBoolean(false);
   cout << baseValue->asBoolean() << endl;

   cout << INDENT2 << "setBinary((void*)\"ABC\", 4)" << INDENT;
   baseValue->setBinary((void*)"ABCEFG", 4);
   cout << baseValue->asBoolean() << " Always false" << endl;

   delete baseValue;
   // End asBoolean   


   cout << INDENT << "TESTING: asBinary" << endl;
   cout << INDENT2 << "WILL ALWAYS RETURN A POINTER TO THE STRUCTURE" << endl;

   printTestFooter("BaseValue Object");
   return 0;
}
